﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Model;

namespace Web.Admin
{
    public partial class Article_Modify : BaseCode.BaseAdmin
    {
        private DAL_Article dalArticle = new DAL_Article();
        private DAL_Tags dalTags = new DAL_Tags();
        protected string oblContent = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int aid = int.Parse(Request.QueryString["id"].ToString());
                TagList();
                bind(aid);
                ViewState["aid"] = aid;
            }
        }
        public void TagList()
        {
            DataTable list = dalTags.GetTagByType(2);
            for (int i = 0; i < list.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Value = list.Rows[i]["id"].ToString();
                li.Text = list.Rows[i]["Name"].ToString();
                li.Attributes.Add("ToopTip", "'" + list.Rows[i]["id"].ToString() + "'");
                this.cblTaglist.Items.Add(li);
            }

            DataTable list2 = dalTags.GetTagByType(1);
            for (int j = 0; j < list2.Rows.Count; j++)
            {
                ListItem li = new ListItem();
                li.Value = list2.Rows[j]["id"].ToString();
                li.Text = list2.Rows[j]["Name"].ToString();
                li.Attributes.Add("ToopTip", "'" + list2.Rows[j]["id"].ToString() + "'");
                this.cblTypelist.Items.Add(li);
            }
        }

        public void bind(int aid)
        {
            DataTable article = dalArticle.GetArticleByID(aid);
            this.txtName.Text = article.Rows[0]["Name"].ToString();
            this.txtEname.Text = article.Rows[0]["EName"].ToString();
            this.txtKeywords.Text = article.Rows[0]["Keywords"].ToString();
            //this.txtAuthor.Text = article.Rows[0]["Author"].ToString();
            //this.txtSourse.Text = article.Rows[0]["Sourse"].ToString();
            //this.txtSourseurl.Text = article.Rows[0]["Sourseurl"].ToString();
            this.txtPublishTime.Text = article.Rows[0]["publishtime"].ToString();
            this.oblContent = article.Rows[0]["Contents"].ToString();
            this.rbtnNoUse.Checked = (int.Parse(article.Rows[0]["State"].ToString()) == 0);
            this.rbtnUse.Checked = (int.Parse(article.Rows[0]["State"].ToString()) == 1);
            for (int i = 0; i < this.cblTaglist.Items.Count; i++)
            {
                if (dalArticle.GetTag_ArtBy(int.Parse(this.cblTaglist.Items[i].Value), aid) > 0)
                {
                    this.cblTaglist.Items[i].Selected = true;
                }
            }

            for (int j = 0; j < this.cblTypelist.Items.Count; j++)
            {
                if (dalArticle.GetTag_ArtBy(int.Parse(this.cblTypelist.Items[j].Value), aid) > 0)
                {
                    this.cblTypelist.Items[j].Selected = true;
                }
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text;
            string ename = this.txtEname.Text;
            //string userid = Session["UserID"].ToString();
            string keyword = this.txtKeywords.Text;
            //string author = this.txtAuthor.Text;
            //string sourse = this.txtSourse.Text;
            //string sourseurl = this.txtSourseurl.Text;
            string contents = this.hiddenContent.Value;
            int state = (this.rbtnNoUse.Checked == true) ? 0 : 1;
            string aid = ViewState["aid"].ToString();
            DateTime publishtime = DateTime.Parse(this.txtPublishTime.Text);

            Model_Article article = dalArticle.GetArticleById(int.Parse(aid));
            article.name = name;
            article.ename = ename;
            article.keywords = keyword;
            article.state = state;
            //article.sourse = sourse;
            //article.sourseurl = sourseurl;
            //article.author = author;
            article.contents = contents;
            article.updatetime = DateTime.Now;
            article.publishTime = publishtime;

            if (dalArticle.UpdateArticle(article))
            {
                for (int i = 0; i < this.cblTaglist.Items.Count; i++)
                {
                    if (this.cblTaglist.Items[i].Selected == true)
                    {
                        if (dalArticle.GetTag_ArtBy(int.Parse(this.cblTaglist.Items[i].Value), int.Parse(aid)) == 0)
                        {
                            dalArticle.AddTag_Art(int.Parse(this.cblTaglist.Items[i].Value), int.Parse(aid));
                        }
                    }
                    else
                    {
                        if (dalArticle.GetTag_ArtBy(int.Parse(this.cblTaglist.Items[i].Value), int.Parse(aid)) > 0)
                        {
                            dalArticle.DeleteTag_art(int.Parse(this.cblTaglist.Items[i].Value), int.Parse(aid));
                        }
                    }
                }

                for (int i = 0; i < this.cblTypelist.Items.Count; i++)
                {
                    if (this.cblTypelist.Items[i].Selected == true)
                    {
                        if (dalArticle.GetTag_ArtBy(int.Parse(this.cblTypelist.Items[i].Value), int.Parse(aid)) == 0)
                        {
                            dalArticle.AddTag_Art(int.Parse(this.cblTypelist.Items[i].Value), int.Parse(aid));
                        }
                    }
                    else
                    {
                        if (dalArticle.GetTag_ArtBy(int.Parse(this.cblTypelist.Items[i].Value), int.Parse(aid)) > 0)
                        {
                            dalArticle.DeleteTag_art(int.Parse(this.cblTypelist.Items[i].Value), int.Parse(aid));
                        }
                    }
                }
                Page.ClientScript.RegisterStartupScript(typeof(string), "提示", "<script>alert('修改成功！');</script>");
                Response.Redirect("Article.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "提示", "<script>alert('修改失败！');</script>");
                return;
                }
            
        }
    }
}